#!/bin/bash
aws s3 cp s3://streaming/flink/emr611-libs/aws-msk-iam-auth-2.0.1-all.jar ~/aws-msk-iam-auth-2.0.1-all.jar
aws s3 cp s3://streaming/flink/emr611-libs/flink-table-api-scala-bridge_2.12-1.16.0.jar ~/flink-table-api-scala-bridge_2.12-1.16.0.jar
aws s3 cp s3://streaming/flink/emr611-libs/flink-table-api-scala_2.12-1.16.0.jar ~/flink-table-api-scala_2.12-1.16.0.jar
aws s3 cp s3://streaming/flink/emr611-libs/flink-connector-kafka-1.16.0.jar ~/flink-connector-kafka-1.16.0.jar
aws s3 cp s3://streaming/flink/emr611-libs/fastjson-1.2.78.jar ~/fastjson-1.2.78.jar
aws s3 cp s3://streaming/flink/emr611-libs/mysql-connector-java-8.0.20.jar ~/mysql-connector-java-8.0.20.jar
aws s3 cp s3://streaming/flink/emr611-libs/druid-1.2.9.jar ~/druid-1.2.9.jar
aws s3 cp s3://streaming/flink/emr611-libs/config-1.4.2.jar ~/config-1.4.2.jar
aws s3 cp s3://streaming/flink/emr611-libs/hudi-flink1.16-bundle-0.14.0.jar ~/hudi-flink1.16-bundle-0.14.0.jar
aws s3 cp s3://streaming/flink/emr611-libs/jedis-4.2.3.jar ~/jedis-4.2.3.jar
aws s3 cp s3://streaming/flink/emr611-libs/zstd-jni-1.5.2-1.jar ~/zstd-jni-1.5.2-1.jar
aws s3 cp s3://streaming/flink/emr611-libs/commons-pool2-2.11.1.jar ~/commons-pool2-2.11.1.jar

sudo cp /usr/lib/hive/auxlib/aws-glue-datacatalog-hive3-client.jar /usr/lib/flink/lib 
sudo cp /usr/lib/hive/lib/antlr-runtime-*.jar /usr/lib/flink/lib 
sudo cp /usr/lib/hive/lib/hive-exec-*.jar /lib/flink/lib 
sudo cp /usr/lib/hive/lib/libfb303-*.jar /lib/flink/lib 
sudo cp /usr/lib/flink/opt/flink-connector-hive_*.jar /lib/flink/lib
# udo cp /usr/lib/hudi/hudi-flink1.16-bundle-0.13.0-amzn-0.jar /lib/flink/lib
sudo cp ~/hudi-flink1.16-bundle-0.14.0.jar  /usr/lib/flink/lib/
sudo cp ~/commons-pool2-2.11.1.jar  /usr/lib/flink/lib/
sudo cp ~/aws-msk-iam-auth-2.0.1-all.jar  /usr/lib/flink/lib/
sudo cp ~/flink-table-api-scala-bridge_2.12-1.16.0.jar /usr/lib/flink/lib/
sudo cp ~/flink-table-api-scala_2.12-1.16.0.jar  /usr/lib/flink/lib/
sudo cp ~/flink-connector-kafka-1.16.0.jar  /usr/lib/flink/lib/
sudo cp ~/fastjson-1.2.78.jar /usr/lib/flink/lib/
sudo cp ~/mysql-connector-java-8.0.20.jar /usr/lib/flink/lib/
sudo cp ~/druid-1.2.9.jar /usr/lib/flink/lib/
sudo cp ~/config-1.4.2.jar  /usr/lib/flink/lib/
sudo cp ~/jedis-4.2.3.jar /usr/lib/flink/lib/
sudo cp ~/zstd-jni-1.5.2-1.jar /usr/lib/flink/lib/
sudo mv /usr/lib/flink/lib/flink-table-planner-loader-1.16.0.jar /usr/lib/flink/opt/
sudo mv /usr/lib/flink/opt/flink-table-planner_2.12-1.16.0.jar /usr/lib/flink/lib/ 

sudo chmod 755 /usr/lib/flink/lib/*
sudo chmod -R 777 /var/log/flink-clis

sudo cp /etc/hive/conf/hive-site.xml /usr/lib/flink/lib/
sudo cp /etc/hive/conf/hive-site.xml /usr/lib/flink/lib/
hdfs dfs -mkdir /completed-jobs

pip3path=`which pip3`

# install pip dependencies
sudo $pip3path install --no-deps smart-open wrapt
rm -rf ~/*.py
rm -rf ~/*.jar
rm -rf ~/*.zip

# download start streaming script
mkdir -p ~/start-streaming
aws s3 cp s3://streaming/flink/script/start_streaming.py ~/start-streaming/
mkdir -p ~/flink-jar
aws s3 cp s3://streaming/flink/jar/hm-iot-streaming.jar ~/flink-jar/hm-iot-streaming.jar
mkdir -p ~/monitor
aws s3 cp s3://streaming/flink/script/monitor.sh /home/hadoop/monitor/
echo '*/30 * * * * source /home/hadoop/.bashrc && aws s3 cp s3://streaming/flink/script/monitor.sh /home/hadoop/monitor/ && nohup /bin/bash /home/hadoop/monitor/monitor.sh 2>&1 &' >> /home/hadoop/cron.cron
crontab /home/hadoop/cron.cron
